home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / gfx / pbm / hpcdtoppm_0_6.lha / hpcdtoppm.0.6 / src / color.c < prev    next >
C/C++ Source or Header  |  1994-10-08  |  5KB  |  248 lines

  1. /* hpcdtoppm (Hadmut's pcdtoppm) v0.6
  2. *  Copyright (c) 1992, 1993, 1994 by Hadmut Danisch (danisch@ira.uka.de).
  3. *  Permission to use and distribute this software and its
  4. *  documentation for noncommercial use and without fee is hereby granted,
  5. *  provided that the above copyright notice appear in all copies and that
  6. *  both that copyright notice and this permission notice appear in
  7. *  supporting documentation. It is not allowed to sell this software in 
  8. *  any way. This software is not public domain.
  9. */
  10.  
  11. #include "hpcdtoppm.h"
  12.  
  13. extern sINT RGB_BitSh1,RGB_Maximum1;
  14. extern sINT RGB_F_LL;
  15. extern sINT RGB_F_C1,RGB_O_C1;
  16. extern sINT RGB_F_C2,RGB_O_C2;
  17. extern sINT RGB_F_G1,RGB_F_G2,RGB_O_G;
  18. extern uBYTE RGB_corr0[],RGB_corr1[],RGB_corr2[];
  19.  
  20.  
  21. static uBYTE *RGB_corr=0;
  22. static sINT T_L[256],T_R[256],T_G[256],T_g[256],T_B[256];
  23.  
  24. #define slen 3072
  25.  
  26.  
  27.  
  28. static void initcorr(void)
  29.  { 
  30.   switch(corrmode)
  31.    {case C_LINEAR: RGB_corr=RGB_corr0; break;
  32.     case C_DARK:   RGB_corr=RGB_corr1; break;
  33.     case C_BRIGHT: RGB_corr=RGB_corr2; break;
  34.     default: error(E_INTERN);
  35.    }
  36.  }
  37.  
  38.  
  39.  
  40. static void initctable(void)
  41.  {sINT i;
  42.   static sINT init=0;
  43.  
  44.   if(init) return;
  45.  
  46.   init=1;
  47.  
  48.   initcorr();
  49.  
  50.   for(i=0;i<256;i++)
  51.    {  T_L[i] = i * RGB_F_LL;
  52.       T_R[i] = i * RGB_F_C2 + RGB_O_C2;
  53.       T_G[i] = i * RGB_F_G1;
  54.       T_g[i] = i * RGB_F_G2 + RGB_O_G;
  55.       T_B[i] = i * RGB_F_C1 + RGB_O_C1;      
  56.    }
  57.   
  58.  }
  59.  
  60.  
  61. static void ycctorgb(implane *l,implane *c1,implane *c2)
  62.  {dim x,y,w,h;
  63.   uBYTE *pl,*pc1,*pc2;
  64.   sINT red,green,blue;
  65.   sINT L;
  66.  
  67.   melde("ycctorgb\n");
  68.   initctable();
  69.  
  70.   w=l->iwidth;
  71.   h=l->iheight;
  72.  
  73.   for(y=0;y<h;y++)
  74.    {
  75.     pl =  l->im + y *  l->mwidth;
  76.     pc1= c1->im + y * c1->mwidth;
  77.     pc2= c2->im + y * c2->mwidth;
  78.  
  79.     for(x=0;x<w;x++)
  80.      {
  81.       L    =  T_L[*pl]; 
  82.       red  = (L + T_R[*pc2]             )>>RGB_BitSh1;
  83.       green= (L + T_G[*pc1] + T_g[*pc2] )>>RGB_BitSh1; 
  84.       blue = (L + T_B[*pc1]             )>>RGB_BitSh1;
  85.  
  86.       red   = TRIF(red,  0,RGB_Maximum1,0,red,  RGB_Maximum1);
  87.       green = TRIF(green,0,RGB_Maximum1,0,green,RGB_Maximum1);
  88.       blue  = TRIF(blue ,0,RGB_Maximum1,0,blue, RGB_Maximum1);
  89.  
  90.       *(pl++ )=RGB_corr[red]; 
  91.       *(pc1++)=RGB_corr[green]; 
  92.       *(pc2++)=RGB_corr[blue];
  93.      }
  94.    }
  95.  }
  96. #undef BitShift
  97.  
  98.  
  99.  
  100.  
  101.  
  102. static void sharpit(implane *l)
  103.  {sINT x,y,h,w,mw,akk;
  104.   uBYTE f1[slen],f2[slen],*old,*akt,*ptr,*work,*help,*optr=0;
  105.  
  106.   melde("sharpit\n");
  107.  
  108.   if((!l) || (!l->im)) error(E_INTERN);
  109.   if(l->iwidth > slen) error(E_INTERN);
  110.  
  111.   old=f1; akt=f2;
  112.   h=l->iheight;
  113.   w=l->iwidth;
  114.   mw=l->mwidth;
  115.  
  116.   for(y=1;y<h-1;y++)
  117.    {
  118.     ptr=l->im+ y*mw;
  119.     optr=ptr-mw;
  120.     work=akt;
  121.  
  122.     *(work++)= *(ptr++);
  123.     for(x=1;x<w-1;x++)
  124.      {  akk = 5*((sINT)ptr[0])- ((sINT)ptr[1])  - ((sINT)ptr[-1]) 
  125.                               - ((sINT)ptr[mw]) - ((sINT)ptr[-mw]);
  126.         NORM(akk);
  127.         *(work++)=akk;
  128.         ptr++;
  129.      }
  130.  
  131.     *(work++)= *(ptr++);
  132.  
  133.     if(y>1) 
  134.       for(x=0;x<w;x++)
  135.         optr[x] = old[x];
  136.  
  137.     help=old;old=akt;akt=help;
  138.      
  139.    }
  140.  
  141.  
  142.  
  143.   akt=optr+mw;
  144.   for(x=0;x<w;x++)
  145.     *(akt++) = *(old++);
  146.  }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. static void initmtable(void)
  155.  {sINT i,h;
  156.   static sINT init=0;
  157.  
  158.   if(init) return;
  159.  
  160.   init=1;
  161.  
  162.   initcorr();
  163.  
  164.   for(i=0;i<256;i++)
  165.    {  h = (i * RGB_F_LL)>>RGB_BitSh1;
  166.       h = TRIF(h,0,RGB_Maximum1,0,h,RGB_Maximum1);
  167.       T_L[i]=RGB_corr[h];
  168.    }
  169.   
  170.  }
  171.  
  172.  
  173. static void monocorr(implane *l)
  174.  {dim x,y,w,h;
  175.   uBYTE *ptr;
  176.  
  177.   melde("monocorr\n");
  178.   initmtable();
  179.  
  180.   w=l->iwidth;
  181.   h=l->iheight;
  182.  
  183.   for(y=0;y<h;y++)
  184.    {
  185.     ptr=  l->im + y *  l->mwidth;
  186.     for(x=0;x<w;x++,ptr++)
  187.      { *ptr = T_L[*ptr]; 
  188.      }
  189.    }
  190.  }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. void colconvert(sizeinfo *si,implane *l,implane *c1,implane *c2)
  199.  
  200. #define w (si->rdhlen)
  201. #define h (si->rdvlen)
  202.  
  203.  {
  204.   melde("colconvert\n");
  205.  
  206.     if((!l ) || ( l->iwidth != w ) || ( l->iheight != h) || (! l->im)) error(E_INTERN);
  207.  
  208.   if(!monochrome)
  209.    {
  210.     if((!c1) || (c1->iwidth != w ) || (c1->iheight != h) || (!c1->im)) error(E_INTERN);
  211.     if((!c2) || (c2->iwidth != w ) || (c2->iheight != h) || (!c2->im)) error(E_INTERN);
  212.    }
  213.  
  214.   if (do_crop)  cropit(si,l,c1,c2);
  215.   else          shrink(si,l,c1,c2);
  216.   if (do_sharp) sharpit(l);
  217.  
  218.   switch (outfor)
  219.    {
  220.     /*  RGB-Conversion */
  221.     case O_PS:
  222.     case O_EPS:
  223.     case O_PPM:
  224.     case O_JPEG:
  225.         ycctorgb(l,c1,c2);
  226.                 break;
  227.  
  228.     /* Grayscale Conversion */
  229.     case O_PSG:
  230.     case O_EPSG:
  231.     case O_PSD:
  232.     case O_EPSD:
  233.     case O_PGM: monocorr(l);
  234.                 break;
  235.  
  236.     /* No Conversion */
  237.     case O_YCC:
  238.                 break;
  239.  
  240.     default: error(E_INTERN);
  241.    }
  242. #undef w
  243. #undef h
  244.  }
  245.  
  246.  
  247.  
  248.